<template>
  <el-dialog title="选择机构" :visible.sync="localOpen" width="800px" :close-on-click-modal="true" @close="cancel"
    append-to-body>
    <div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="queryParams.contacts" placeholder="请输入联系人" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="机构名称" prop="organizationName">
          <el-input v-model="queryParams.organizationName" placeholder="请输入机构名称" clearable
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="上线" prop="state">
          <el-select v-model="queryParams.state" placeholder="请选择上线" clearable>
            <el-option v-for="dict in dict.type.common_state" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="省、市、区" prop="queryProvinceAndCityAndDistrict">
          <el-cascader placeholder="请选择省、市、区" v-model="queryParams.queryProvinceAndCityAndDistrict" :options="options"
            @keyup.enter.native="handleQuery" @lazy-load="selectLoadOptions"
            :props="{ lazy: true,checkStrictly:true,multiple: true, lazyLoad: selectLoadOptions,value:'regionId',label:'regionName' }"
            clearable></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" ref="selectOrganizationTable" :data="organizationList"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="id" align="center" prop="id" />
        <el-table-column label="联系人" align="center" prop="contacts" />
        <el-table-column label="机构名称" align="center" prop="organizationName" />
        <el-table-column label="联系电话" align="center" prop="tel" />
        <el-table-column label="省、市、区" align="center" prop="provinceAndCityAndDistrict" />
        <el-table-column label="上线" align="center" prop="state">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.common_state" :value="scope.row.state" />
          </template>
        </el-table-column>
        <el-table-column label="招生人数" align="center" prop="stuCount" />
        <el-table-column label="所属代理" align="center" prop="agentName" />
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="selectOrgData">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>

  </el-dialog>
</template>

<script>
  import {
    listOrganization,
  } from "@/api/bus_web/agent/organization";
  import {
    loadOptions
  } from "@/api/system/sys_region";
  import store from "@/store";
  import {
    mapState
  } from 'vuex';
  export default {
    name: "SelectOrganization",
    dicts: ['common_state'],
    props: {
      open: Boolean
    },
    data() {
      return {
        // 遮罩层
        localOpen: this.open,
        loading: true,
        options: [],
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 机构信息表格数据
        organizationList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          contacts: null,
          organizationName: null,
          state: null,
          stuCount: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    watch: {
      open(newValue, oldValue) {
        this.localOpen = newValue; // 同步父组件的 open 值到本地状态
      },
      localOpen(newValue) {
        if (newValue) {
          this.rows = []
          this.getList() // 当本地状态变为 false 时，通知父组件
        } else {
          this.cancel()
        }
      }
    },
    computed: {
      ...mapState({
        organization: state => state.selectOrganization.organization,
        organizationSelectModel: state => state.selectOrganization.organizationSelectModel
      })
    },
    methods: {
      /**省 市 区*/
      selectLoadOptions(node, resolve) {
        var query = {
          regionLevel: node.level + 1,
          parentId: node.value || null
        }
        loadOptions(query).then(response => {
          resolve(response.data);
        });
      },
      /** 查询机构信息列表 */
      getList() {
        this.loading = true;
        listOrganization(this.queryParams).then(response => {
          this.organizationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.$emit('close-select-window')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        if (selection.length > 1 && this.organizationSelectModel == 2) {
          selection = []
          this.$refs.selectOrganizationTable.clearSelection()
          this.$store.dispatch('selectOrganization/cleanOrganizationData')
          this.$modal.msgError("最多只能选择1条数据")
          return
        }
        selection.map(item => {
          this.$store.dispatch('selectOrganization/setOrganization', item)
        })
      },
      //确定选择数据
      selectOrgData() {
        if (this.organization.length < 1) {
          this.$modal.msgError("请至少选中1条机构")
          return
        }
        this.$emit('selected-data')
      },
    }
  };
</script>
